package com.kaifamiao.contraller;

import com.kaifamiao.model.UserModel;
import com.kaifamiao.service.UserService;
import com.kaifamiao.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService service;

    @RequestMapping("/add")
    public int add(){
        UserModel model=new UserModel();
//        model.setId(5L);
        model.setAccountname("sssss");
        model.setPassword("123123");
        model.setName("ddd");
        model.setLogtimes(6);
        model.setSex("nv");
        model.setTouxiang("");
        model.setRegdate(null);
        model.setPhone("22333333");
        return service.addUser(model);
    }

    @RequestMapping("/update")
    @CrossOrigin("*")
    public ResponseUtil update(UserModel model1){

        UserModel model=service.findById(model1.getId());
        model.setAccountname(model1.getAccountname());
        model.setPassword(model1.getPassword());
        model.setName(model1.getName());
        model.setLogtimes(model1.getLogtimes());
        model.setSex(model1.getSex());
        model.setTouxiang(model1.getTouxiang());
        model.setRegdate(null);
        model.setPhone(model1.getPhone());
        int row= service.updateUser(model);
        if(row>0){
            return ResponseUtil.ok("修改成功");
        }else{
            return ResponseUtil.error("修改失败");
        }
    }

    @RequestMapping("/updatePassword")
    @CrossOrigin("*")
    public ResponseUtil updatePassword(UserModel model1){
        UserModel model=service.findById(model1.getId());
        System.out.println(model);
        model.setPassword(model1.getPassword());

        int row = service.updateUser(model);
        if(row > 0){
            return ResponseUtil.ok("修改成功");
        }else{
            return ResponseUtil.error("修改失败");
        }
    }

    @RequestMapping("/delete")
    @CrossOrigin("*")
    public ResponseUtil delete(@RequestParam(value = "id",required = false) Long id){
        int row = service.deleteUser(id);
        if(row > 0){
            return ResponseUtil.ok("修改成功");
        }else{
            return ResponseUtil.error("修改失败");
        }
    }

    @RequestMapping("/find")
    public ModelAndView find(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",service.findById(5L));
        modelAndView.setViewName("/WEB-INF/success.jsp");
        return modelAndView;
    }


    @RequestMapping("/findAll")
    @CrossOrigin("*")
    public ResponseUtil findAll(UserModel model){
        List<UserModel> list=service.findAll();
        return ResponseUtil.ok("",list);
    }


    @RequestMapping("/select")
    public void select(String accountname){
        System.out.println(service.selectByAccountName(accountname));

    }

    @RequestMapping(value = "/register")
    @CrossOrigin
    public ResponseUtil register(UserModel userModel){
        List list = service.findByUser(userModel);
        if (list.size() > 0){
            return ResponseUtil.error("用户名已存在");
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            Date date = calendar.getTime();
            String dateStringParse = sdf.format(date);
            userModel.setRegdate(dateStringParse);
            userModel.setLogtimes(0);
            userModel.setTouxiang("/static/img/u704.png");
            userModel.setRoleId(1);
            service.addUser(userModel);
            return ResponseUtil.ok("注册成功");
        }
    }


    @RequestMapping(value = "/login")
    @CrossOrigin("*")
    public ResponseUtil login(@RequestParam(value = "accountname",required = false) String accountname,
                              @RequestParam(value = "password",required = false)String password){
        UserModel userModel = service.selectByAccountName(accountname);
        if(userModel != null && password.equals(userModel.getPassword())){
            return ResponseUtil.ok("请求成功",userModel);
        }else{
            return ResponseUtil.error("请求失败");
        }
    }
}
